package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.fragment.MasterClassFragment;
import com.yuejian.neusoft.yuejian_24.fragment.MasterQuestionFragment;
import com.yuejian.neusoft.yuejian_24.javaBean.Collected;
import com.yuejian.neusoft.yuejian_24.javaBean.Master;
import com.yuejian.neusoft.yuejian_24.javaBean.Success;
import com.yuejian.neusoft.yuejian_24.listener.CollectedListener;
import com.yuejian.neusoft.yuejian_24.listener.MasterListener;
import com.yuejian.neusoft.yuejian_24.listener.SuccessListener;
import com.yuejian.neusoft.yuejian_24.model.MasterModel;


public class MasterDetailActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView club,introduction;
    private TextView masterClass,masterQusetion;
    private Fragment mTab01,mTab02;
    private ImageView masterimg,back,collect;
    private Intent intent;
    private Context mContext;
    private SharedPreferences sp;

    MasterListener masterListener = new MasterListener() {
        @Override
        public void onResponse(Master info) {
            club.setText(info.getMaster_job());
            introduction.setText("\u3000\u3000"+info.getMaster_content());
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/photo/"+info.getMaster_pic()).into(masterimg);
        }

        @Override
        public void onFail() {

        }
    };

    CollectedListener collectedListener = new CollectedListener() {
        @Override
        public void onResponse(Collected collected) {
            if (collected.getCollected().equals("1")){
                collect.setImageResource(R.drawable.ic_collect1);
            }
            else if (collected.getCollected().equals("0")){
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener followListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(MasterDetailActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect1);
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(MasterDetailActivity.this,"取消关注成功", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);
        initViews();
        initEvent();
        MasterModel masterModel = new MasterModel();
        masterModel.getmaster(sp.getString("mid",""),masterListener);
        masterModel.isCollected(sp.getString("mid",""),sp.getString("uid",""),"3",collectedListener);
        setSelect(0);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                if (mTab01 == null)
                {
                    mTab01 = new MasterClassFragment();
                    transaction.add(R.id.id_content2, mTab01);
                } else
                {
                    transaction.show(mTab01);
                }
                break;
            case 1:
                if (mTab02 == null)
                {
                    mTab02 = new MasterQuestionFragment();
                    transaction.add(R.id.id_content2, mTab02);
                } else
                {
                    transaction.show(mTab02);
                }
                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
    }
    private void initViews() {
        club = (TextView)findViewById(R.id.ma_detail_club);
        introduction = (TextView)findViewById(R.id.ma_detail_introduction);
        masterimg = (ImageView)findViewById(R.id.ma_detail_img);
        back = (ImageView)findViewById(R.id.ma_detail_back);
        collect = (ImageView)findViewById(R.id.ma_detail_collect);
        masterClass = (TextView)findViewById(R.id.ma_detail_class);
        masterQusetion = (TextView)findViewById(R.id.ma_detail_question);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }
    private void initEvent() {
        back.setOnClickListener(this);
        masterClass.setOnClickListener(this);
        masterQusetion.setOnClickListener(this);
        collect.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ma_detail_class:
                masterClass.setTextColor(Color.parseColor("#000000"));
                masterQusetion.setTextColor(Color.parseColor("#ffffff"));
                setSelect(0);
                break;

            case R.id.ma_detail_question:
                masterClass.setTextColor(Color.parseColor("#ffffff"));
                masterQusetion.setTextColor(Color.parseColor("#000000"));
                setSelect(1);
                break;

            case R.id.ma_detail_back:
                finish();
                break;

            case R.id.ma_detail_collect:
                MasterModel masterModel = new MasterModel();
                masterModel.follow(sp.getString("mid",""),sp.getString("uid",""),"3",followListener);
                break;
        }
    }



}
